//
//  ShowPhotoView.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/10/13.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "ShowPhotoView.h"
#import "ShowPhotoCell.h"
#import "SDPhotoBrowser.h"

@interface ShowPhotoView ()<UICollectionViewDataSource, UICollectionViewDelegate, SDPhotoBrowserDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation ShowPhotoView

- (void)awakeFromNib {
    
    [super awakeFromNib];
    [self setupUI];
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.userInteractionEnabled = YES;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collectionView];
    
    [_collectionView registerClass:[ShowPhotoCell class] forCellWithReuseIdentifier:@"ShowPhotoCell"];
    
    @weakify(self);
    [_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        
        CGFloat width = 76 * self.dataSource.count + 10 + 10;
        if (width > CGRectGetWidth(self.bounds)) {
            width = CGRectGetWidth(self.bounds);
        }
        
        make.width.mas_equalTo(width);
        
    }];;
}

- (void)setDataSource:(NSArray *)dataSource {
    
    _dataSource = dataSource;
    @weakify(self);
    [_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        
        CGFloat width = 76 * self.dataSource.count + 10 + 10;
        if (width > CGRectGetWidth(self.bounds)) {
            width = CGRectGetWidth(self.bounds);
        }
        
        make.width.mas_equalTo(width);
        
    }];
    [self.collectionView reloadData];
}

#pragma mark UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ShowPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShowPhotoCell" forIndexPath:indexPath];
    
    NSString *thubUrl = self.dataSource[indexPath.row];
    
    [cell.imageView setImageURL:[NSURL URLWithString: kQiNiuHeightLock(thubUrl,300)] placeholderImage:[UIImage imageNamed:@"placeholder_squ"]];

    cell.deleteBtn.hidden = YES;
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    
    browser.sourceImagesContainerView = collectionView;
    browser.currentImageIndex = indexPath.row;
    browser.imageCount = self.dataSource.count;
    
    browser.delegate = self;
    [browser show];
}
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    
    NSString *picstr = self.dataSource[index];
    
    return [UIImageView imageFromDiskCacheForKey:kQiNiuHeightLock(picstr,300)];
}
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    
    NSString *picstr = self.dataSource[index];
    
    return [NSURL URLWithString:picstr];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(76, 76);
}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

@end
